package com.learn.search.service.impl;

import com.alibaba.fastjson.JSON;
import com.learn.search.dao.ElasticSearchDAO;
import com.learn.search.entity.ElasticEntity;
import com.learn.search.feign.CourseFeign;
import com.learn.search.service.ICourseService;
import com.learn.eduapi.entity.Course;
import com.learn.eduapi.entity.PageEntity;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class CourseServiceImpl implements ICourseService {

    @Autowired
    private ElasticSearchDAO dao;

    @Autowired
    private CourseFeign courseFeign;

    @Override
    public List<Course> initCourseIndex(){
        try {
            if(dao.existsIndex(INDEX_NAME)) {
                dao.deleteIndex(INDEX_NAME);
            }
            dao.createIndex(INDEX_NAME);
            List<Course> allCourse = courseFeign.getAllCourse();
            List<ElasticEntity> list = new ArrayList<>();
            allCourse.forEach(course -> {
                list.add(new ElasticEntity(String.valueOf(course.getId()),course));
            });
            dao.insertBatch(INDEX_NAME,list);
            return allCourse;
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Override
    public void saveOrUpdate(Course course){
        try {
            dao.saveOrUpdate(INDEX_NAME,new ElasticEntity(String.valueOf(course.getId()),course));
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Override
    public void removeById(Long id) {
        try {
            dao.deleteByQuery(INDEX_NAME,QueryBuilders.termQuery("id", id));
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Course> searchByCourseName(String courseName){
        SearchSourceBuilder query = new SearchSourceBuilder()
                .query(QueryBuilders.matchQuery("courseName", courseName));
        try {
            List<Course> courses = dao.search(INDEX_NAME, query, Course.class);
            return courses;
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Override
    public PageEntity<Course> searchCoursePage(Map<String, String> map){
        try {
            int current = 1;
            int size = 10;
            Map<String,String> search = null;
            Map<String,String> sort = null;
            if(map.containsKey("current")){
                current = Integer.valueOf(map.get("current"));
            }
            if(map.containsKey("size")){
                size = Integer.valueOf(map.get("size"));
            }
            if(map.containsKey("search")){
                search = JSON.parseObject(map.get("search"),Map.class);
            }
            if(map.containsKey("sort")){
                sort = JSON.parseObject(map.get("sort"),Map.class);
            }
            PageEntity<Course> page = dao.searchPageByMap(INDEX_NAME, (current - 1) * size, size, search,sort, Course.class);
            return page;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
